<?php
/**
 * Csv出力アクションヘルパ
 *
 * @package	Controller_Action_Helper
 * @subpackage	CsvExporter
 * @author		tanaka
 */
class Controller_Action_Helper_CsvExporter extends Zend_Controller_Action_Helper_Abstract
{
	/**
	 * CSVを出力します。
	 * 
	 * @param File_Csv_CreatorAbstract $csv
	 * @return string
	 */
	public function export(File_Csv_CreatorAbstract $csv)
	{
		$data = $csv->getCsv();
		$filename = $csv->getFileName();
		
		$data = mb_convert_encoding($data, 'sjis-win', 'utf-8');
		$filename = mb_convert_encoding($filename, 'sjis-win', 'utf-8');
		
		$response = $this->getResponse();
		$response->clearHeaders();
		$response->clearBody();
		$response->setRawHeader('Content-Type: text/csv');
		$response->setRawHeader('Content-Disposition: attachment; filename="'. $filename . '"');
		$response->setRawHeader('Content-Transfer-Encoding: binary');
		$response->setRawHeader('Expires: 0');
		$response->setRawHeader('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		$response->setRawHeader('Pragma: public');
		$response->setRawHeader('Content-Length: '.strlen($data));
		$response->setBody($data);
		$response->sendResponse();
		exit;
	}
	
	/**
	 * ダイレクト
	 * 
	 * @param File_Csv_CreatorAbstract $csv
	 * @return string
	 */
	public function direct(File_Csv_CreatorAbstract $csv)
	{
		return $this->export($csv);
	}
}